/* ====================================================================
 * Copyright (C) 2012 - Deniz Sezen
 * ====================================================================
 * dx_main.cpp - This is where all globals and other variables are setup
 *  for the main mod dll.
 * ====================================================================
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/************************************************************************/
/* Includes                                                             */
/************************************************************************/

/* DX Includes. */
#include "dx_augsystem.h"

/* Operating system includes. */
#include <windows.h>
#include <direct.h>
#include <stdio.h>

/************************************************************************/
/* Global variables                                                     */
/************************************************************************/

/* Address of the file object. */
FILE* g_pLogFile = NULL;

/************************************************************************/
/* Finds signatures that are required by this module.					*/
/************************************************************************/
bool InitializeSignatures( void )
{
	Init_AugSystem();

	return true;
}

/************************************************************************/
/* Sets up global variables.                                            */
/************************************************************************/
bool InitializeGlobals( void )
{
	// Flush output to file.
	fflush(g_pLogFile);

	// Done.
	return true;
}

/************************************************************************/
/* Dumps objects from g_pObjects.                                       */
/************************************************************************/
void DumpGlobalObjects( void )
{
	// Make sure augsystem is up and running.
	while( !g_pAugmentationSystem )
		Sleep(100);


	for( int i = 0; i < 64; i++ ) {
		fprintf(g_pLogFile, "Upgrade: %s\n", g_pAugmentationSystem->m_Objects[i]->Name);
		fprintf(g_pLogFile, " Value: %f\n", *g_pAugmentationSystem->m_Objects[i]->pValue);
	}

	UpgradeDescriptor_Float* Energy = g_pAugmentationSystem->FindObject("SprintDuration");
	if( !Energy ) {
		fprintf(g_pLogFile, "Could not find SprintDuration!\n");
		return;
	}

	*Energy->pValue = 20.0f;

	// Flush output to file.
	fflush(g_pLogFile);
}

/************************************************************************/
/* Initializes the entire module.                                       */
/************************************************************************/
void InitializeModule( void )
{
	// Open up a file handle.
	g_pLogFile = fopen("data.txt", "w");

	// Setup signatures.
	if( !InitializeSignatures() || !InitializeGlobals() ) {
		MessageBox(NULL, "Could not initialize the module!", "Error!", MB_ICONERROR);
		return;
	}

	while ( !GetAsyncKeyState( VK_INSERT ) )
		Sleep( 100 );

	// Dump all of the objects found in the script array.
	DumpGlobalObjects();
}

/************************************************************************/
/* The main thread for the dll.											*/
/************************************************************************/
unsigned long ModuleThread( void* )
{
	RTL_CRITICAL_SECTION CriticalSection;
	InitializeCriticalSection( &CriticalSection );
	EnterCriticalSection( &CriticalSection );

	InitializeModule();

	LeaveCriticalSection( &CriticalSection );
	DeleteCriticalSection( &CriticalSection );

	return 0;
}

/************************************************************************/
/* Main entry point for the dll.                                        */
/************************************************************************/
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved )
{
	// Perform actions based on the reason for calling.
	switch( fdwReason ) 
	{ 
	case DLL_PROCESS_ATTACH:
		{
			// Disables PROCESS_ATTACH notifications.
			DisableThreadLibraryCalls((HMODULE)hinstDLL);

			// Always do your initialization in a separate thread. Never
			// in DllMain().
			CreateThread( 0, 0, (LPTHREAD_START_ROUTINE) ModuleThread, 0, 0, 0 );

			// Initialize once for each new process.
			// Return FALSE to fail DLL load.
			break;
		}

	case DLL_PROCESS_DETACH:
		{
			// Close out the file.
			if( g_pLogFile )
				fclose(g_pLogFile);
		}
	}

	return TRUE;  // Successful DLL_PROCESS_ATTACH.
}
